<template>
  <div class="lists">
    <el-table
      :data="specsListData"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
    >
      <el-table-column prop="id" label="编号" sortable width="180">
      </el-table-column>
      <el-table-column prop="specsname" label="名称" sortable width="180">
      </el-table-column>
      <el-table-column prop="attrs" label="规格属性" width="400">
        <template slot-scope="scope">
          <el-tag
            type="success"
            v-for="(item, index) in scope.row.attrs"
            :key="index"
            >{{ item }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 1">启用</el-tag>
          <el-tag v-if="scope.row.status == 2" type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" @click="editSpecs(scope.row.id)">编辑</el-button>
          <el-button type="danger" @click="deleteSpecs(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total - 0"
      :page-size="listQuery.size"
      :current-page="listQuery.page"
      @current-change="curChange"
    >
    </el-pagination>
  </div>
</template>

<script>
import {
  getSpecsList,
  getSpecsCount,
  getSpecsDelete,
} from "../../../request/api";
export default {
  props:['dialog'],
  data() {
    return {
      // 商品规格总数
      total: "",
      listQuery: {
        page: 1,
        size: 2,
      },
      // 商品规格列表数据
      specsListData: [],
    };
  },
  methods: {
    // 获取商品规格总数
    getSpecsCountData() {
      getSpecsCount().then(
        (res) => {
          if (res.data.code == 200) {
            this.total = res.data.list[0].total;
          }
        },
        (err) => err
      );
    },
    // 获取商品规格列表
    getSpecsListData() {
      getSpecsList(this.listQuery).then(
        (res) => {
          if (res.data.code == 200) {
            this.specsListData = res.data.list;
          }
        },
        (err) => err
      );
    },
    // 切换页码触发
    curChange(page) {
      this.listQuery.page = page;
      this.getSpecsListData();
    },
    // 点击编辑展示对话框数据
    editSpecs(id){
      this.$emit('editSpecs',id)
    },
    // 删除商品规格
    deleteSpecs(id) {
      this.$confirm("是否删除该菜单?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      }).then(
        () => {
          getSpecsDelete({ id }).then(
            (res) => {
              if (res.data.code == 200) {
                this.$message.success("删除成功");
                this.dialog.isShowDialog = false;
                // 刷新列表数据
                this.listQuery.page=1
                this.getSpecsListData();
              }
            },
            (err) => err
          );
        },
        (err) => err
      );
    },
  },
  mounted() {
    this.getSpecsCountData();
    this.getSpecsListData();
  },
};
</script>

<style scoped>
.el-tag {
  margin-right: 10px;
  margin-bottom: 5px;
}
.el-pagination {
  margin-left: 950px;
}
</style>